﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mjudoku
{
    class SudokuChecker
    {

        int[,] valueArray;
        int size, blockSize;

        public SudokuChecker(int[,] valueArray, int size, int blockSize) {
            this.valueArray = valueArray;
            this.size = size;
            this.blockSize = blockSize;
        }

        public bool checkBlocks() 
        {
            for (int i = 0; i < this.blockSize; i++)
            {
                for (int j = 0; j < this.blockSize; j++)
                {
                    if (!checkBlock(i, j)) return false;
        }
            }
            return true;
        }

        public bool checkRows() {
            for (int i = 0; i < size; i++)
            {
                if (!checkRow(i)) return false;
            }
            return true;
        }

        private bool checkRow(int rowNumber) {
            bool[] contains = new bool[this.size+1];//length = size + 1 - contains indexes from 1 to last

            for (int i = 0; i < contains.Length; i++)
            {
                contains[i] = false;
            }

            for (int i = 0; i < this.size; i++)
            {
                int actualIndex = valueArray[i, rowNumber];
                if (contains[actualIndex]) {
                    return false;
                }
                contains[actualIndex] = true;
            }
            return true;
        }
        //kontrola jednotlivých řádků

        public bool checkColumns()
        {
            for (int i = 0; i < size; i++)
            {
                if (!checkColumn(i)) return false;
            }
            return true;
        }

        private bool checkColumn(int columnNumber) {
            bool[] contains = new bool[this.size + 1];//length = size + 1 - contains indexes from 1 to last

            for (int i = 0; i < contains.Length; i++)
            {
                contains[i] = false;
            }

            for (int i = 0; i < this.size; i++)
            {
                int actualIndex = valueArray[columnNumber, i];
                if (contains[actualIndex])
                {
                    return false;
                }
                contains[actualIndex] = true;
            }
            return true;
        }

        //řádkový index a sloupcový index - nejsou to řádky, ale index bloku v řádcích
        private bool checkBlock(int rowIndex, int columnIndex) {
            bool[] contains = new bool[this.size + 1];//length = size + 1 - contains indexes from 1 to last

            for (int i = 0; i < contains.Length; i++)
            {
                contains[i] = false;
            }

            for (int i = rowIndex * this.blockSize; i < this.blockSize; i++)
            {
                for (int j = columnIndex * this.blockSize; j < this.blockSize; j++)
                {
                    int actualIndex = valueArray[i, j];
                    if (contains[actualIndex])
                    {
                        return false;
                    }
                    contains[actualIndex] = true;
                }
            }            
            return true;
        }
    }
}
